package com.heima.ssm.dao;

import com.heima.ssm.pojo.Role;
import com.heima.ssm.pojo.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Penguin
 * @create Date 2021/5/5  Time 18:28
 */
public interface IUserDao {

    @Select("select * from users where username=#{username}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),
            @Result(property = "roles",column = "id",javaType = List.class,
                    many = @Many(select = "com.heima.ssm.dao.IRoleDao.findRoleByRoleIdWhereFromUsersRoleRelationship"))
    })
    public UserInfo findByUsername(String username);

    @Select("select * from users")
    List<UserInfo> findAll();

    @Insert("insert into users(email,username,password,phoneNum,status) values(#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo);

    @Select("select * from users where id = #{userId}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),
            @Result(property = "roles",column = "id",javaType = List.class,
                    many = @Many(select = "com.heima.ssm.dao.IRoleDao.findRoleByRoleIdWhereFromUsersRoleRelationship"))
    })
    UserInfo findById(int userId);

    //根据用户id查询当前用户没有但可以添加的角色
    @Select("select * from role where id not in (select roleId from users_role where userId = #{userId})")
    List<Role> findOtherRoleCouldAddToUser(int userId);

    //将选中的角色id赋给user，也就是将俩参数传给users_role表单
    @Insert("insert into users_role( userId , roleId ) values( #{userId} , #{roleId})")
    void addRoleToUser(@Param("userId") int userId, @Param("roleId") int roleId);
}
